package com.longcai.service.impl.nearby;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.nearby.ClyjNearbyGoodsType;
import com.longcai.mapper.nearby.ClyjNearbyGoodsTypeMapper;
import com.longcai.service.nearby.ClyjNearbyGoodsTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 附近商品类型 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-03-26
 */
@Service
public class ClyjNearbyGoodsTypeServiceImpl extends ServiceImpl<ClyjNearbyGoodsTypeMapper, ClyjNearbyGoodsType> implements ClyjNearbyGoodsTypeService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjNearbyGoodsTypeMapper clyjNearbyGoodsTypeMapper;

    /**
     * 查询店铺商品类型对应商品
     *
     * @param shopsid
     * @param type
     * @return
     */
    @Override
    public ApiResult getShopsGoods(String shopsid, String type) {

        try {
            List<ClyjNearbyGoodsType> list = this.clyjNearbyGoodsTypeMapper.getShopsGood(shopsid, type);
            logger.debug("查询成功+++++++++++++++", list);
            return ApiResult.success(list);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败"+e.getMessage());
        }
    }

    /**
     * 查询店铺商品类型
     *
     * @param shopsid
     * @return
     */
    @Override
    public ApiResult getShoptype(String shopsid) {


        try {
            QueryWrapper<ClyjNearbyGoodsType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("shop_id", shopsid);
            queryWrapper.isNull("pid");
            List<ClyjNearbyGoodsType> list = this.clyjNearbyGoodsTypeMapper.selectList(queryWrapper);
            logger.debug("查询成功+++++++++++++++", list);
            return ApiResult.success(list);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取行业类型
     * @return
     */
    public ApiResult getShopsGoodsByArea(){
        //查询行业一级类型
        QueryWrapper<ClyjNearbyGoodsType> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNull("pid");
        queryWrapper.orderByAsc("type_sort");
        List<ClyjNearbyGoodsType> list = this.clyjNearbyGoodsTypeMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<String, Object>();
        if(list != null && list.size() > 0){
            for (ClyjNearbyGoodsType clyjNearbyGoodsType: list) {
                QueryWrapper<ClyjNearbyGoodsType> queryWrapperSec = new QueryWrapper<>();
                queryWrapperSec.eq("pid",clyjNearbyGoodsType.getId());
                queryWrapperSec.orderByAsc("type_sort");
                List<ClyjNearbyGoodsType> clyjNearbyGoodsTypeList = this.clyjNearbyGoodsTypeMapper.selectList(queryWrapperSec);
                map.put(clyjNearbyGoodsType.getName(),clyjNearbyGoodsTypeList);
            }
        }
        return ApiResult.success(map);
    }
}
